package com.minivision.sdktestdemo.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.RemoteViews;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.event.PlayMusicEvent;
import com.minivision.sdktestdemo.service.MusicService;
import com.minivision.sdktestdemo.ui.StartActivity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by devtang on 2016/4/23.
 */
public class MusicControlNotification {
    private Context mContext;
    private RemoteViews mRemoteView;
    private RemoteViews mBigRemoteView;

    public static final int NOTIFY_UPDATE = 1;

    public static final int ACTION_INIT = 2;

    public static final int ACTION_NEXT = 3;

    public static final int ACTION_PLAY_CHANGE = 4;

    public static final int ACTION_CLOSE = 5;

    public static final int ACTION_PREV = 6;

    public static final int ACTION_LYC = 7;

    public static final long TIME_STAMP = 1234567890l;

    public static final String STATUS = "status";

    public MusicControlNotification(Context context) {
        mContext = context;
    }

    public RemoteViews getRemoteView(String title, String content, Uri uri, int status, boolean isBigStyle) {
        RemoteViews remoteViews = null;
        if (isBigStyle) {
            if (mBigRemoteView == null) {
                mBigRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.notification_expend_music);
                //初始化按键的监听器
                initButtonListener(mBigRemoteView, true);
            }
            remoteViews = mBigRemoteView;
        } else {
            if (mRemoteView == null) {
                mRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.notification_music);
                //初始化按键的监听器
                initButtonListener(mRemoteView, false);
            }
            remoteViews = mRemoteView;
        }
        //设定notification的界面
        initPlayControl(remoteViews, uri, title, content, status, true);
        return remoteViews;
    }

    public void initAlbumPic(RemoteViews remoteViews, Uri uri) {
        //初始化专辑图片
        InputStream is = null;
        try {
            if (!uri.equals(Uri.EMPTY) && (is = mContext.getContentResolver().openInputStream(uri)) != null) {
                remoteViews.setImageViewUri(R.id.icon_music, uri);
            } else {
                remoteViews.setImageViewResource(R.id.icon_music, R.drawable.placeholder_disk_150);
            }
        } catch (FileNotFoundException e) {
            remoteViews.setImageViewResource(R.id.icon_music, R.drawable.placeholder_disk_150);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void initPlayControl(RemoteViews remoteViews, Uri uri, String title, String content, int status, boolean isBigStyle) {
        //设定notification的专辑图片
        initAlbumPic(remoteViews, uri);
        //设定其他界面的图片和文字
        remoteViews.setTextViewText(R.id.tv_title, title);
        remoteViews.setTextViewText(R.id.tv_content, content);
        remoteViews.setImageViewResource(R.id.btn_play,
                status == PlayMusicEvent.PLAY ? R.drawable.note_btn_pause : R.drawable.note_btn_play);
        remoteViews.setImageViewResource(R.id.btn_play_next, R.drawable.note_btn_next);
        remoteViews.setImageViewResource(R.id.btn_cancel, R.drawable.note_btn_close);
        remoteViews.setImageViewResource(R.id.btn_show, R.drawable.note_btn_lyc);
        if (isBigStyle) {
            remoteViews.setImageViewResource(R.id.btn_prev, R.drawable.note_btn_pre);
        }
    }

    public void initButtonListener(RemoteViews remoteViews, boolean isBigStyle) {
        //设定播放/暂停的响应
        remoteViews.setOnClickPendingIntent(R.id.btn_play, getClickPendingIntent(ACTION_PLAY_CHANGE));

        //设定下一首的响应
        remoteViews.setOnClickPendingIntent(R.id.btn_next, getClickPendingIntent(ACTION_NEXT));

        //设定关闭服务的响应
        remoteViews.setOnClickPendingIntent(R.id.btn_cancel, getClickPendingIntent(ACTION_CLOSE));

        //设置歌词显示的响应
        remoteViews.setOnClickPendingIntent(R.id.btn_show, getClickPendingIntent(ACTION_LYC));
        if (isBigStyle) {
            //设置上一首的响应
            remoteViews.setOnClickPendingIntent(R.id.btn_prev, getClickPendingIntent(ACTION_PREV));
        }
    }

    public PendingIntent getClickPendingIntent(int status) {
        Intent intent = new Intent(mContext, MusicService.class);
        intent.putExtra(STATUS, status);
        PendingIntent pendingIntent = PendingIntent.getService(mContext, status, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    public void close() {
        NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(NOTIFY_UPDATE);
    }

    public void updateNotification(String title, String content, Uri uri, int status, boolean isShowLyc) {
        Intent resultIntent = new Intent(mContext, StartActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        resultIntent.setAction(Intent.ACTION_MAIN);
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
        stackBuilder.addParentStack(StartActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationCompat.Builder(mContext).
                setSmallIcon(R.mipmap.ic_launcher).setContentTitle("音乐正在播放").setPriority(Notification.PRIORITY_MAX).build();
        notification.contentView = getRemoteView(title, content, uri, status, false);
        notification.bigContentView = getRemoteView(title, content, uri, status, true);
        notification.contentIntent = resultPendingIntent;
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.when = TIME_STAMP;
        NotificationManager mNotificationManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFY_UPDATE, notification);
    }
}
